/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

package org.apache.kylin.invertedindex;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.kylin.invertedindex.model.IIDesc;
import org.apache.kylin.metadata.model.FunctionDesc;
import org.apache.kylin.metadata.model.JoinDesc;
import org.apache.kylin.metadata.model.LookupDesc;
import org.apache.kylin.metadata.model.TableDesc;
import org.apache.kylin.metadata.model.TblColRef;
import org.apache.kylin.metadata.realization.CapabilityResult;
import org.apache.kylin.metadata.realization.SQLDigest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Sets;

/**
 */
public class IICapabilityChecker {
    private static final Logger logger = LoggerFactory.getLogger(IICapabilityChecker.class);

    public static CapabilityResult check(IIInstance ii, SQLDigest digest) {
        CapabilityResult result = new CapabilityResult();
        result.capable = false;

        // match fact table
        if (!digest.factTable.equalsIgnoreCase(ii.getFactTable())) {
            logger.info("Exclude II " + ii.getName() + " because fact table unmatch");
            return result;
        }
        
        // match joins
        boolean matchJoin = isMatchedWithJoins(digest.joinDescs, ii);
        if (!matchJoin) {
            logger.info("Exclude II " + ii.getName() + " because unmatched joins");
            return result;
        }

        // dimensions & measures
        Collection<TblColRef> dimensionColumns = getDimensionColumns(digest);
        Collection<FunctionDesc> aggrFunctions = digest.aggregations;
        Collection<TblColRef> unmatchedDimensions = unmatchedDimensions(dimensionColumns, ii);
        Collection<FunctionDesc> unmatchedAggregations = unmatchedAggregations(aggrFunctions, ii);
        
        // try dimension-as-measure
        if (!unmatchedAggregations.isEmpty()) {
            tryDimensionAsMeasures(unmatchedAggregations, digest, ii, result);
        }
        
        if (!unmatchedDimensions.isEmpty()) {
            logger.info("Exclude ii " + ii.getName() + " because unmatched dimensions");
            return result;
        }
        
        if (!unmatchedAggregations.isEmpty()) {
            logger.info("Exclude ii " + ii.getName() + " because unmatched aggregations");
            return result;
        }

        // cost will be minded by caller
        result.capable = true;
        return result;
    }

    private static boolean isMatchedWithJoins(Collection<JoinDesc> joins, IIInstance iiInstance) {
        IIDesc iiDesc = iiInstance.getDescriptor();
        List<TableDesc> tables = iiDesc.listTables();

        List<JoinDesc> cubeJoins = new ArrayList<JoinDesc>(tables.size());
        for (TableDesc tableDesc : tables) {
            JoinDesc join = null;
            for (LookupDesc lookup : iiDesc.getModel().getLookups()) {
                if (lookup.getTable().equalsIgnoreCase(tableDesc.getIdentity())) {
                    join = lookup.getJoin();
                    cubeJoins.add(join);
                    break;
                }
            }
        }

        for (JoinDesc j : joins) {
            // optiq engine can't decide which one is fk or pk
            String pTable = j.getPrimaryKeyColumns()[0].getTable();
            String factTable = iiDesc.getModel().getFactTable();
            if (factTable.equals(pTable)) {
                j.swapPKFK();
            }

            // check primary key, all PK column should refer to same tale, the Fact Table of iiInstance.
            // Using first column's table name to check.
            String fTable = j.getForeignKeyColumns()[0].getTable();
            if (!factTable.equals(fTable)) {
                logger.info("Fact Table" + factTable + " not matched in join: " + j + " on ii " + iiInstance.getName());
                return false;
            }

            // The hashcode() function of JoinDesc has been overwritten,
            // which takes into consideration: pk,fk,jointype
            if (!cubeJoins.contains(j)) {
                logger.info("Query joins don't match on ii " + iiInstance.getName());
                return false;
            }
        }
        return true;
    }

    private static Collection<TblColRef> getDimensionColumns(SQLDigest sqlDigest) {
        Collection<TblColRef> groupByColumns = sqlDigest.groupbyColumns;
        Collection<TblColRef> filterColumns = sqlDigest.filterColumns;

        Collection<TblColRef> dimensionColumns = new HashSet<TblColRef>();
        dimensionColumns.addAll(groupByColumns);
        dimensionColumns.addAll(filterColumns);
        return dimensionColumns;
    }

    private static Set<TblColRef> unmatchedDimensions(Collection<TblColRef> dimensionColumns, IIInstance ii) {
        HashSet<TblColRef> result = Sets.newHashSet(dimensionColumns);
        result.removeAll(ii.getDescriptor().listAllDimensions());
        return result;
    }

    private static Set<FunctionDesc> unmatchedAggregations(Collection<FunctionDesc> aggregations, IIInstance ii) {
        HashSet<FunctionDesc> result = Sets.newHashSet(aggregations);
        result.removeAll(ii.getDescriptor().listAllFunctions());
        return result;
    }

    private static void tryDimensionAsMeasures(Collection<FunctionDesc> unmatchedAggregations, SQLDigest digest, IIInstance ii, CapabilityResult result) {
        IIDesc iiDesc = ii.getDescriptor();
        Collection<FunctionDesc> iiFuncs = iiDesc.listAllFunctions();

        Iterator<FunctionDesc> it = unmatchedAggregations.iterator();
        while (it.hasNext()) {
            FunctionDesc functionDesc = it.next();
            
            if (iiFuncs.contains(functionDesc)) {
                it.remove();
                continue;
            }

            // let calcite handle count
            if (functionDesc.isCount()) {
                it.remove();
                continue;
            }

            // calcite can do aggregation from columns on-the-fly
            List<TblColRef> neededCols = functionDesc.getParameter().getColRefs();
            if (neededCols.size() > 0 && iiDesc.listAllDimensions().containsAll(neededCols)) {
                result.influences.add(new CapabilityResult.DimensionAsMeasure(functionDesc));
                it.remove();
                continue;
            }
        }
    }

}
